<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增广告管理')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: jasny-bootstrap-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-advertising-add">
            <input name="platform" th:value="${platform}" type="hidden">
            <div class="form-group">    
                <label class="col-sm-3 control-label">应用ID：</label>
                <div class="col-sm-8">
                    <select name="appId" class="form-control m-b" th:with="type=${@dict.getType('kkad_app_id')}" style="pointer-events: none;">
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:selected="${appId} == ${dict.dictValue}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">广告位置：</label>
                <div class="col-sm-8">
                    <select id="placeCode" name="placeCode"  class="form-control m-b" required>
                       <option value="">请选择位置</option>
                       <option th:each="place : ${placeList}" th:text="${place['name']}" th:value="${place['code']}" th:attr="data-width=${place['showWidth']},data-height=${place['showHeight']}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">计费类型：</label>
                <div class="col-sm-8">
                    <select name="billingType" class="form-control m-b" th:with="type=${@dict.getType('kkad_billing_type')}">
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" ></option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">启用：</label>
                <div class="col-sm-8">
                     <label class="toggle-switch switch-solid">
                     <input type="checkbox" id="status" >
                     <span></span>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">测试：</label>
                <div class="col-sm-8">
                    <label class="toggle-switch switch-solid">
                    <input type="checkbox" id="test" >
                    <span></span>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">跳转URL：</label>
                <div class="col-sm-8">
                    <input name="targetUrl" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">跳转类型：</label>
                <div class="col-sm-8">
                    <select name="targetType" class="form-control m-b" th:with="type=${@dict.getType('kkad_target_type')}">
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">上线时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        <input name="effectStart" class="form-control" placeholder="yyyy-MM-dd HH:mm:ss" type="text" required>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">下线时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        <input name="effectEnd" class="form-control" placeholder="yyyy-MM-dd HH:mm:ss" type="text" required>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">文字描述：</label>
                <div class="col-sm-8">
                    <input name="content" class="form-control" type="text required">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">备注：</label>
                <div class="col-sm-8">
                    <input name="remark" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">序号：</label>
                <div class="col-sm-8">
                    <input name="sequence" class="form-control" type="text" value="1" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">推广目标(万次展示)：</label>
                <div class="col-sm-8">
                    <input name="goal" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">图片：</label>
                <div class="col-sm-8">
                    <input id="imgUri" name="imgUri"  class="form-control" type="hidden">
	                <div class="fileinput fileinput-new" data-provides="fileinput">
				        <div class="fileinput-new thumbnail" style="width: 80px; height: 80px;" id="fileinput-new">
				          
				        </div>
				        <div class="fileinput-preview fileinput-exists thumbnail" style="max-width: 80px; max-height:80px;" id="fileinput-preview"></div>
				        <div>
				          <span class="btn btn-white btn-file"><span class="fileinput-new">选择图片</span><span class="fileinput-exists">更改</span><input type="file" class="image" ></span>
				          <a href="#" class="btn btn-white fileinput-exists" data-dismiss="fileinput">清除</a>
				        </div>
				     </div>
                </div>
            </div>
              
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: jasny-bootstrap-js" />
    <script type="text/javascript">
        var prefix = ctx + "kkad/advertising"
        $("#form-advertising-add").validate({
            focusCleanup: true
        });
        
        $('.image').on('change.bs.fileinput', function (e) {
        	var file = $(this)[0].files[0];
        	var data = new FormData();
	        data.append("file", file);
	        $.ajax({
	            type: "POST",
	            url: ctx + "common/uploadOSS",
	            data: data,
	            cache: false,
	            contentType: false,
	            processData: false,
	            dataType: 'json',
	            success: function(result) {
	            	$('#imgUri').val(result.url);
	            },
	            error: function(error) {
	                alert("图片上传失败。");
	            }
	        });
        	
        });
        
        $('#placeCode').change(function(){
        	var option = $("#placeCode option:selected");
        	var width = option.data('width');
        	var height = option.data('height');
        	
        	$('#fileinput-new').css({
               'width': width + 'px',
               'height': height + 'px'
            });
        	$('#fileinput-preview').css({
        	   'max-width': width + 'px',
               'max-height': height + 'px'
        	})
        })

        function submitHandler() {
            if ($.validate.form()) {
            	
            	var data = $('#form-advertising-add').serializeArray()
            	var status = $("input[id='status']").is(':checked') == true ? 1 : 0;
            	data.push({"name": "status", "value": status});
            	var test = $("input[id='test']").is(':checked') == true ? 1 : 0;
            	data.push({"name": "test", "value": test});
            	
                $.operate.save(prefix + "/add", data);
            }
        }

        $("input[name='effectStart']").datetimepicker({
            format: "yyyy-mm-dd hh:ii:00",
            autoclose: true
        });

        $("input[name='effectEnd']").datetimepicker({
            format: "yyyy-mm-dd hh:ii:00",
            autoclose: true
        });

    </script>
</body>
</html>